home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Skunkware 5
/
Skunkware 5.iso
/
man
/
cat.3
/
scandir.3
< prev
next >
Wrap
Text File
|
1995-07-27
|
6KB
|
199 lines
SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))
NNNNAAAAMMMMEEEE
scandir, alphasort - scan a directory
SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ttttyyyyppppeeeessss....hhhh>>>>
####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ddddiiiirrrr....hhhh>>>>
iiiinnnntttt
ssssccccaaaannnnddddiiiirrrr((((nnnnaaaammmmeeee,,,, lllliiiisssstttt,,,, sssseeeelllleeeeccccttttoooorrrr,,,, ssssoooorrrrtttteeeerrrr))))
cccchhhhaaaarrrr ****nnnnaaaammmmeeee;;;;
ssssttttrrrruuuucccctttt ddddiiiirrrreeeecccctttt ************lllliiiisssstttt;;;;
iiiinnnntttt ((((****sssseeeelllleeeeccccttttoooorrrr))))(((())));;;;
iiiinnnntttt ((((****ssssoooorrrrtttteeeerrrr))))(((())));;;;
iiiinnnntttt
aaaallllpppphhhhaaaassssoooorrrrtttt((((dddd1111,,,, dddd2222))))
ssssttttrrrruuuucccctttt ddddiiiirrrreeeecccctttt ********dddd1111;;;;
ssssttttrrrruuuucccctttt ddddiiiirrrreeeecccctttt ********dddd2222;;;;
DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
_S_c_a_n_d_i_r reads the directory _n_a_m_e and builds a
NULL-terminated array of pointers to the entries found in
that directory. This array is put into the location pointed
to by the _l_i_s_t parameter.
If the _s_e_l_e_c_t_o_r parameter is non-NULL, it is taken to be a
pointer to a function called with each entry, to determine
whether or not it should be included in the returned list.
If the parameter is NULL, all entries are included.
As an added feature, the entries can be sorted (with
_q_s_o_r_t(3)) before the list is returned. If the _s_o_r_t_e_r
parameter is non-NULL, it is passed to qsort to use as the
comparison function. The _a_l_p_h_a_s_o_r_t routine is provided to
sort the array alphabetically.
The array pointed to by _l_i_s_t and the items it points to are
all space obtained through _m_a_l_l_o_c(3), and their storage can
be reclaimed as shown in the example below.
EEEEXXXXAAAAMMMMPPPPLLLLEEEE
Page 1 (printed 7/27/95)
SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))
Here is a small _l_s(1)-like program:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>
extern int alphasort();
static int
filesonly(e)
struct direct *e;
{
struct stat sb;
return(stat(e->d_name, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFREG);
}
main(ac, av)
int ac;
char *av[];
{
register int i;
register int j;
struct direct **list;
if (ac != 2) {
fprintf(stderr, "usage: %s dirname0, av[0]);
exit(1);
}
if (chdir(av[1]) < 0) {
perror(av[1]);
exit(1);
}
if ((i = scandir(".", &list, filesonly, alphasort)) < 0) {
perror("Error reading directory");
exit(1);
}
for (j = 0; j < i; j++)
printf("%s0, list[j]->d_name);
for (j = 0; j < i; j++)
free((char *)list[j]);
free((char *)list);
exit(0);
}
SSSSEEEEEEEE AAAALLLLSSSSOOOO
directory(3), qsort(3)
DDDDIIIIAAAAGGGGNNNNOOOOSSSSTTTTIIIICCCCSSSS
Returns the number of entries in the ``list,'' or -1 if the
directory could not be opened or a memory allocation failed.
Page 2 (printed 7/27/95)
SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV SSSSCCCCAAAANNNNDDDDIIIIRRRR((((3333))))
BBBBUUUUGGGGSSSS
The routine can be slightly wasteful of space.
Page 3 (printed 7/27/95)